﻿using AMAP.Areas.Client.Models.Account;
using AMAP.ServiceInterfaces;
using System;
using System.Web.Mvc;
using System.Web.Security;

namespace AMAP.Areas.Client.Controllers
{
    public class AccountController : Controller
    {
        private IClientService ClientService;
        private ICoordinatesService CoordinatesService;
        private IPlaceService PlaceService;
        private IUserService UserService;

        public AccountController(IClientService ClientService, ICoordinatesService CoordinatesService, IPlaceService PlaceService, IUserService UserService)
        {
            this.ClientService = ClientService;
            this.CoordinatesService = CoordinatesService;
            this.PlaceService = PlaceService;
            this.UserService = UserService;
        }

        //
        // GET: /Client/Account/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create(bool Discover = false)
        {
            ClientAccountCreateViewModel Model = new ClientAccountCreateViewModel();
            Model.PlaceList = new SelectList(PlaceService.GetAllAsList(), "Id", "Address1");
            Model.Discover = Discover;
            return View(Model);
        }

        [HttpPost]
        public ActionResult Create(ClientAccountCreateViewModel Model)
        {
            if (ModelState.IsValid)
            {
                Domain.Client Client = ClientService.Create();

                AutoMapper.Mapper.Map(Model, Client);
                AutoMapper.Mapper.Map(Model, Client.User);

                ClientService.Add(Client);
                //Connect the registrated user
                FormsAuthentication.SetAuthCookie(Model.Email, false);

                EmailService.Send("florent.henry@gmail.com", "Inscription client - Les marrons", "Bonjour Florent,<br>Votre inscription a bien été enregistrée.");
                if (Model.Discover)
                    return RedirectToRoute("Client_default", new { controller = "Order", action = "Basket" });
                else
                    return RedirectToRoute("Client_default", new { controller = "Home", action = "Index" });
            }
            else
                Model.PlaceList = new SelectList(PlaceService.GetAllAsList(), "Id", "Address1");
            return View(Model);
        }

        [Authorize(Roles = "Client")]
        public ActionResult Edit()
        {
            throw new NotImplementedException();
        }

        [Authorize(Roles = "Client")]
        public ActionResult AddCoordinates()
        {
            ClientAccountAddCoordinatesViewModel Model = new ClientAccountAddCoordinatesViewModel();
            return View(Model);
        }

        [HttpPost]
        [Authorize(Roles = "Client")]
        public ActionResult AddCoordinates(ClientAccountAddCoordinatesViewModel Model)
        {
            Domain.User Client = UserService.GetByEmail(User.Identity.Name);
            Client.Coordinates = new Domain.Coordinates();

            AutoMapper.Mapper.Map(Model, Client.Coordinates);
            Client.Coordinates = GeoCoder.SetGPSCoordinates(Client.Coordinates);
            UserService.Save();

            return RedirectToRoute("Client_default", new { controller = "Home", action = "Index" });
        }

        [Authorize(Roles = "Client")]
        public ActionResult EditCoordinates()
        {
            ClientAccountEditCoordinatesViewModel Model = new ClientAccountEditCoordinatesViewModel();
            Domain.User Client = this.UserService.GetByEmail(User.Identity.Name);
            AutoMapper.Mapper.Map(Client.Coordinates, Model);
            return View(Model);
        }

        [HttpPost]
        [Authorize(Roles = "Client")]
        public ActionResult EditCoordinates(ClientAccountEditCoordinatesViewModel Model)
        {
            Domain.Coordinates Coordinates = this.UserService.GetByEmail(User.Identity.Name).Coordinates;

            //Due to EF4 issues, we cannot automap ViewModel content into the Coordinates POCO
            //(which isn't really a POCO in this case but a EF Proxy object)

            Coordinates.Address1 = Model.Address1;
            Coordinates.Address2 = Model.Address2;
            Coordinates.City = Model.City;
            Coordinates.PhoneNumber = Model.PhoneNumber;
            Coordinates.PostalCode = Model.PostalCode;
            Coordinates = GeoCoder.SetGPSCoordinates(Coordinates);
            this.CoordinatesService.Update(Coordinates);

            return RedirectToRoute("Client_default", new { controller = "Home", action = "Index" });
        }
    }
}
